package com.keqiang.chart.impl.radar.impl

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.Drawable
import androidx.annotation.ColorInt
import androidx.annotation.FloatRange
import androidx.annotation.Px
import com.keqiang.chart.base.BaseChartView
import com.keqiang.chart.impl.radar.BaseRadarChart
import com.keqiang.chart.impl.radar.BaseTouchHandler
import com.keqiang.chart.impl.radar.XAxis
import com.keqiang.chart.impl.radar.YAxis
import com.keqiang.chart.impl.radar.render.IAxisRender
import com.keqiang.chart.impl.radar.render.IDataRender
import com.keqiang.chart.impl.radar.render.XAxisRender
import com.keqiang.chart.impl.radar.render.YAxisRender

/**
 * 雷达图
 *
 * @author Created by wanggaowan on 2022/10/27 13:12
 */
open class RadarChart(view: BaseChartView<*>) : BaseRadarChart<List<RadarData>>(view) {

    var xAxis: XAxis = XAxis()
    var yAxis: YAxis = YAxis()

    override var xAxisRender: IAxisRender = XAxisRender(this)
    override var yAxisRender: IAxisRender = YAxisRender(this)
    override var dataRender: IDataRender = RadarChartDataRender(this)
    override var touchHandler: BaseTouchHandler<out BaseRadarChart<List<RadarData>>, List<RadarEntity>>? =
        RadarTouchHandler(this)

    var highLightRender: HighLightRender? = null

    override fun isEmptyData(): Boolean {
        if (data.isNullOrEmpty()) {
            return true
        }

        var isEmpty = true
        for (d in data!!) {
            if (d.values.isNotEmpty()) {
                isEmpty = false
                break
            }
        }
        return isEmpty
    }
}

/**
 * 折线图数据
 */
open class RadarData(
    /**
     * 需要绘制的值
     */
    var values: List<RadarEntity>
) {

    /**
     * 绘制折线宽度
     */
    @setparam:Px
    @setparam:FloatRange(from = 1.0)
    var lineWidth: Float = 1f

    /**
     * 绘制折线颜色
     */
    @setparam:ColorInt
    var lineColor: Int = Color.GRAY

    /**
     * 是否填充背景
     */
    var fillBg: Boolean = false

    /**
     * 填充的背景颜色，如果设置了此值也设置了[fillBgDrawable],则优先使用[fillBgDrawable]。
     */
    var fillBgColor: Int = 0

    /**
     * 填充的背景图像
     */
    var fillBgDrawable: Drawable? = null
}

/**
 * 高亮数据绘制器
 *
 * @author Created by wanggaowan on 2022/10/31 10:11
 */
abstract class HighLightRender(val chart: RadarChart) {
    /**
     * 绘制高亮内容
     */
    abstract fun onDraw(canvas: Canvas, data: RadarEntity)
}